// this ts for auth

import router from './router/index'
import store from './store/index'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import {
    getStore,
  } from '@/utils/store'
router.beforeEach(async (to, from, next) => {
    const token =getStore({ name: 'token'})
    NProgress.start()
    console.log(token)
    console.log(to)
    if (token) {
        if (to.path === '/') {
            console.log(48548)
            next({ path: '/home' })
            NProgress.done()
        } else {
            if (!store.state.token) {
                // set authority
                // await store.dispatch('setAuthority')
                // it's a hack func,avoid bug
                next({ ...to, replace: true })
            } else {
                next()
            }
        }
    } else {
        console.log(to)
        if (to.path !== '/login') {
            console.log(898989)
            next({ path: '/login' })
        } else {
            console.log(55555)
            next(true)
        }
    }
})

router.afterEach(() => {
    NProgress.done()
})
